<script>

import { getImgCaptcha, getPhoneCaptcha, login } from '@/api/login'
import { Toast } from 'vant'
import { mapMutations } from 'vuex'
// 15751776629
export default {
  name: 'loginIndex',
  data () {
    return {
      captchaImage: '', // 用于存储验证码图片的URL
      captchaCode: '', // 用于存储验证码文本
      captchaImgText: '', // 用于存储用户输入的验证码文本
      captchaSmsText: '', // 用于存储用户输入的短信验证码文本
      phoneNumber: '', // 用于存储用户输入的手机号码
      totalSeconds: 10, // 用于存储倒计时的总秒数
      Seconds: 10, // 用于存储倒计时的秒数
      timer: null // 用于存储定时器的引用
    }
  },
  async created () {
    await this.shuaxin()
  },
  methods: {
    async shuaxin () {
      const res = await getImgCaptcha()
      console.log(res)
      this.captchaImage = res.data.base64
      this.captchaCode = res.data.key
    },
    async PhoneCaptcha () {
      if (this.phoneNumber.length < 11) {
        Toast('请输入正确的手机号码')
        return
      }
      if (this.Seconds === 10) {
        this.$refs.clickCaptcha.disabled = true
      }
      console.log('我被点击了')
      if (!this.timer && this.Seconds === this.totalSeconds) {
        this.timer = setInterval(() => {
          if (this.Seconds > 0) {
            this.Seconds--
          } else {
            clearInterval(this.timer)
            this.timer = null
            this.Seconds = this.totalSeconds
            this.$refs.clickCaptcha.disabled = false
          }
        }, 1000)
      }
      const res = await getPhoneCaptcha(this.phoneNumber, this.captchaImage, this.captchaCode)
      Toast('发送成功！')
      console.log(res)
    },
    async clickLogin () {
      const res = await login(this.phoneNumber, this.captchaSmsText)
      console.log(res)
      if (res.status === 200) {
        Toast('登录成功！')
        this.setUserInfo({ token: res.data.token, userId: res.data.userId })
        const url = this.$router.query.redirect || '/'
        await this.$router.replace(url)
      }
    },
    ...mapMutations('user', ['setUserInfo'])
  },
  destroyed () {
    clearInterval(this.timer)
  }
}
</script>

<template>
  <div>
    <div class="login">
    <van-nav-bar title="会员登录" left-arrow @click-left="$router.go(-1)" />
    <div class="container">
      <div class="title">
        <h3>手机号登录</h3>
        <p>未注册的手机号登录后将自动注册</p>
      </div>

      <div class="form">
        <div class="form-item">
          <input class="inp" maxlength="11" placeholder="请输入手机号码" type="text" v-model="phoneNumber">
        </div>
        <div class="form-item">
          <input class="inp" maxlength="5" placeholder="请输入图形验证码" type="text" v-model="captchaImgText">
          <img :src="captchaImage" alt="" @click="shuaxin">
        </div>
        <div class="form-item">
          <input class="inp" placeholder="请输入短信验证码" type="text" v-model="captchaSmsText">
          <button @click="PhoneCaptcha" ref="clickCaptcha">{{ this.Seconds === this.totalSeconds ? '获取验证码' : this.Seconds + '秒后获取' }}</button>
        </div>
      </div>
      <div class="login-btn" @click="clickLogin">登录</div>
    </div>
  </div>
  </div>
</template>

<style lang="less" scoped>
.container {
  padding: 49px 29px;

  .title {
    margin-bottom: 20px;
    h3 {
      font-size: 26px;
      font-weight: normal;
    }
    p {
      line-height: 40px;
      font-size: 14px;
      color: #b8b8b8;
    }
  }

  .form-item {
    border-bottom: 1px solid #f3f1f2;
    padding: 8px;
    margin-bottom: 14px;
    display: flex;
    align-items: center;
    .inp {
      display: block;
      border: none;
      outline: none;
      height: 32px;
      font-size: 14px;
      flex: 1;
    }
    img {
      width: 94px;
      height: 31px;
    }
    button {
      height: 31px;
      border: none;
      font-size: 13px;
      color: #cea26a;
      background-color: transparent;
      padding-right: 9px;
    }
  }

  .login-btn {
    width: 100%;
    height: 42px;
    margin-top: 39px;
    background: linear-gradient(90deg,#ecb53c,#ff9211);
    color: #fff;
    border-radius: 39px;
    box-shadow: 0 10px 20px 0 rgba(0,0,0,.1);
    letter-spacing: 2px;
    display: flex;
    justify-content: center;
    align-items: center;
  }
}
</style>
